// 打算写一个自动 根据文件 ，变成接口的

const fs = require('fs');
const path = require('path')

const excludes = ['.DS_Store', 'README.md', 'readme.md']

function AutoApi() {
  getFileName('', '')
}

function getFileName(prefix, subdir, apiArray = []) {
  const children = [];
  const fileTypes = /\.js$/; // 只匹配以js结尾的文件
  const rpath = path.join(prefix, subdir)
  fs.readdirSync(rpath).forEach(file => {
    if (excludes.indexOf(file) < 0) {
      const fullpath = rpath + '/' + file
      const fileinfo = fs.statSync(fullpath)
      if (fileinfo.isFile()) {
        if (fileTypes.test(file) > 0) {
          const apiFilePath = path.join(subdir, file)
          const apiFileIntro = '/' + file
          const api = { path: apiFilePath, intro: apiFileIntro }
          children.push(api);
        }
      } else if (fileinfo.isDirectory()) {
        const chilSubdir = path.join(subdir, '/' + file + '/')
        const childrenObj = getFileName(prefix, chilSubdir)
        apiArray.push(childrenObj)
      }
    }
  })
  // console.log(children)
  children.sort(); // 排序
  return children;
}

module.exports = AutoApi